package com.yang.crawler.html;


import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
 * 使用模板模式，将WebClient的创建逻辑抽取出来
 * @param <Result>
 */
public abstract class AbstractHtmlUnitTemplate<Result> {
    protected String url;

    public AbstractHtmlUnitTemplate(String url) {
        this.url = url;
    }

    public Result reptile() {
        try (WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setActiveXNative(false);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0");
            webClient.setAjaxController(new NicelyResynchronizingAjaxController());

            HtmlPage indexPage = webClient.getPage(this.url);
            webClient.waitForBackgroundJavaScript(30000);

            return handleHtmlPage(indexPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public abstract Result handleHtmlPage(HtmlPage page);
}
